package com.ngc.mall.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ngc.mall.common.configure.LocalUser;
import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.domain.UserAddressDomain;
import com.ngc.mall.param.AddressParam;
import com.ngc.mall.service.IUserAddressService;
import com.ngc.mall.service.IWxAuthenticationService;
import com.ngc.mall.service.IWxUserService;
import com.ngc.mall.vo.WxUserVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/wechat")
public class UserController {

    @Resource
    IWxAuthenticationService service;

    @Resource
    IWxUserService iWxUserService;

    @Resource
    IUserAddressService iUserAddressService;


    @GetMapping("/login")
    @ApiOperation("微信授权登录")
    public String authLogin() {
//        return ResponseResult.auto(service.updateUserWxInfo(param));
        return "后端返回";
    }

    /**
     * 获取用户个人信息
     */
    @GetMapping("/userinfo")
    @ApiOperation("获取用户个人信息")
    public ResponseResult<WxUserVO> getUserInfo(){
        Long uid=LocalUser.getUser().getId();
        return ResponseResult.success(iWxUserService.getDetailById(uid));
    }

    /**
     * 新建收货地址
     */
    @PostMapping("/address")
    public ResponseResult<Boolean> createAddress(@RequestBody AddressParam addressParam){
        Long id=LocalUser.getUser().getId();
        UserAddressDomain userAddressDomain=UserAddressDomain.builder()
                .userName(addressParam.getUsername())
                .mobile(addressParam.getMobile())
                .detail(addressParam.getDetail())
                .province(addressParam.getProvince())
                .country(addressParam.getCountry())
                .city(addressParam.getCity())
                .userId(id)
                .isDefault(addressParam.getIsDefault())
                .build();
        if(addressParam.getIsDefault()){
            updateDefaultAddress();
        }
        return ResponseResult.auto(iUserAddressService.save(userAddressDomain));
    }

    public void updateDefaultAddress(){
        Long id=LocalUser.getUser().getId();
        Wrapper wrapper=Wrappers.<UserAddressDomain>lambdaQuery().eq(UserAddressDomain::getUserId,id).eq(UserAddressDomain::getIsDefault,true);
        List<UserAddressDomain> list = iUserAddressService.list(wrapper);
        List<UserAddressDomain> newList=new ArrayList<>();
        for(UserAddressDomain user : list){
            user.setIsDefault(false);
            newList.add(user);
        }
        iUserAddressService.updateBatchById(newList);
    }

    /**
     * 编辑收货地址
     */
    @PutMapping("/address/{id}")
    public ResponseResult<Boolean> updateAddressById(@PathVariable Long id, @RequestBody AddressParam addressParam){
        Wrapper wrapper=Wrappers.<UserAddressDomain>lambdaUpdate()
                .eq(UserAddressDomain::getId,id)
                .set(UserAddressDomain::getUserName,addressParam.getUsername())
                .set(UserAddressDomain::getCity, addressParam.getCity())
                .set(UserAddressDomain::getCountry,addressParam.getCountry())
                .set(UserAddressDomain::getProvince,addressParam.getProvince())
                .set(UserAddressDomain::getDetail,addressParam.getDetail())
                .set(UserAddressDomain::getIsDefault,addressParam.getIsDefault())
                .set(UserAddressDomain::getMobile,addressParam.getMobile());
        if(addressParam.getIsDefault()){
            updateDefaultAddress();
        }
        return ResponseResult.auto(iUserAddressService.update(wrapper));
    }

    /**
     * 获取当前登录用户的收货地址列表
     */
    @GetMapping("/address/list")
    public ResponseResult<List<UserAddressDomain>> list(){
        Long userId=LocalUser.getUser().getId();
        Wrapper wrapper=Wrappers.<UserAddressDomain>lambdaQuery().eq(UserAddressDomain::getUserId,userId);
        return ResponseResult.success(iUserAddressService.list(wrapper));
    }

    /**
     * 删除指定id收货地址
     */
    @DeleteMapping("/address/{id}")
    public ResponseResult<Boolean> deleteAddressById(@PathVariable Long id){
            return ResponseResult.auto(iUserAddressService.removeById(id));
    }

    /**
     * 获取默认收货地址
     */
    @GetMapping("/address/default")
    public ResponseResult<UserAddressDomain> getDefaultAddress(){
        Long uid=LocalUser.getUser().getId();
        Wrapper wrapper=Wrappers.<UserAddressDomain>lambdaQuery().eq(UserAddressDomain::getUserId,uid).eq(UserAddressDomain::getIsDefault,true);
        return ResponseResult.success(iUserAddressService.getOne(wrapper));
    }


}
